#include <SDL/SDL_image.h>
#include "MainWindow.h"
#include "Gomoku.h"
#include "PieceSelector.h"

PieceSelector& PieceSelector::m_instance = PieceSelector();

PieceSelector::PieceSelector()
: m_selectedPiece(NULL), m_caseBackup(COLOR_NONE)
{
}

PieceSelector::~PieceSelector()
{
}

PieceSelector& PieceSelector::Instance()
{
	return (PieceSelector::m_instance);
}

bool PieceSelector::draw(SDL_Surface* _screen)
{
	if (m_selectedPiece != NULL)
	{
		SDL_BlitSurface(m_selectedPiece->Surface, NULL, _screen, &m_position);
		return (true);
	}
	return (false);
}

void PieceSelector::doEvents()
{
	SDL_Event& e = MainWindow::Event();

	if (m_selectedPiece != NULL)
	{
		int x = 0, y = 0;
		Uint8 mouseState = SDL_GetMouseState(&x, &y);
		m_position.x = x - m_selectedPiece->Surface->w / 2 - 16;
		m_position.y = y - m_selectedPiece->Surface->h / 2 - 16;
		if (m_position.x <= 0)
			m_position.x = 0;
		else if (m_position.x >= (Board::WIDTH - 1) * m_selectedPiece->Surface->w)
			m_position.x = (Board::WIDTH - 1) * m_selectedPiece->Surface->w;
		if (m_position.y <= 0)
			m_position.y = 0;
		else if (m_position.y >= (Board::HEIGHT - 1) * m_selectedPiece->Surface->h)
			m_position.y = (Board::HEIGHT - 1) * m_selectedPiece->Surface->h;
		
		BoardCase** cases = Gomoku::Instance().Board.Cases;
		if (e.type == SDL_MOUSEBUTTONUP && e.button.button == SDL_BUTTON_LEFT)//mouseState&SDL_BUTTON(1))
		{
			SDL_Rect newPos = { 0, 0, -1, -1 };
			newPos.x = (m_position.x + 16) / 32;
			newPos.y = (m_position.y + 16) / 32;

			if (newPos.x >= Board::WIDTH || newPos.y >= Board::HEIGHT)
				return ;
			
			BoardCase color = GET_COLOR(cases[m_boardPosition.y][m_boardPosition.x]);
			if (Gomoku::Instance().Board.CanIMove(newPos.x, newPos.y, color, true) == true)
			{
				Gomoku& gomoku = Gomoku::Instance();
				gomoku.Board.CapturePieces(cases[newPos.y][newPos.x]);
				BoardCase& tmp = cases[m_boardPosition.y][m_boardPosition.x];
				m_caseBackup = COLOR_NONE;
				Gomoku::Instance().Board.ClearCase(tmp, m_boardPosition.x, m_boardPosition.y);
				m_selectedPiece = NULL;
				if (gomoku.Arbiter.HasCurrentPlayerWon() == true)
				{
					gomoku.getPlayers()[color].Winner = true;
					gomoku.Arbiter.SetCurrentPlayerWin(false);
				}
				if (gomoku.getPlayers()[color].EnnemyPiecesCount >= 10)
					gomoku.getPlayers()[color].Winner = true;
			}
			e.type = SDL_MOUSEMOTION;
			SDL_PushEvent(&e);
		}
		else 
		{
			Uint8* keystate = SDL_GetKeyState(NULL);
			if (keystate[SDLK_ESCAPE])
			{
				BoardCase& tmp = cases[m_boardPosition.y][m_boardPosition.x];
				Board::CreateNewBoardCase(tmp, GET_COLOR(m_caseBackup), GET_POSITION_X(m_caseBackup), GET_POSITION_Y(m_caseBackup), GET_ALIGN_ENMY(m_caseBackup), GET_ALIGN_ALLY(m_caseBackup), GET_SURROUNDED(m_caseBackup));
				m_selectedPiece = NULL;
				BoardCase caseBackup = m_caseBackup;
				m_caseBackup = COLOR_NONE;
			}
		}
	}
}

Image* PieceSelector::getSelectedPiece()
{
	return (m_selectedPiece);
}

void PieceSelector::setSelectedPiece(Image* _selectedPiece)
{
	m_selectedPiece = _selectedPiece;
	if (_selectedPiece != NULL)
		m_position = m_selectedPiece->Position;
}

void PieceSelector::setBoardPosition(BoardCase _x, BoardCase _y)
{
	m_boardPosition.x = _x;
	m_boardPosition.y = _y;
}

SDL_Rect const& PieceSelector::getSelectedPosition() const
{
	return (m_selectedPiece->Position);
}

void PieceSelector::setCaseBackup(BoardCase& _case)
{
	m_caseBackup = _case;
}

BoardCase const& PieceSelector::getCaseBackup() const
{
	return (m_caseBackup);
}
